package com.fenxiangle.yueding.feature.message.view;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.FoucsListBo;
import com.fenxiangle.yueding.entity.bo.MsgBo;
import com.fenxiangle.yueding.feature.focus.contract.FocusContract;
import com.fenxiangle.yueding.feature.focus.contract.MsgContract;
import com.fenxiangle.yueding.feature.focus.view.adapter.FocusAdapter;
import com.fenxiangle.yueding.feature.message.dependencies.msg.DaggerMsgComponent;
import com.fenxiangle.yueding.feature.message.dependencies.msg.MsgPresenterModule;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.EmptyView;
import com.suozhang.framework.widget.SpacesItemDecoration;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderMsgActivity extends BaseActivity implements
        OnRefreshListener, BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener,MsgContract.View
{


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_msg_order)
    RecyclerView mRvMsgMsg;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;

    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<MsgBo> mData;
    private OrderMsgAdapter mAdapter;

    @Inject
    MsgContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order_msg;
    }

    @Override
    protected void initInjector() {
        DaggerMsgComponent.builder().msgPresenterModule(new MsgPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar,"订单消息",true,true);
        initAdapter();

    }

    public void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvMsgMsg);
        mRvMsgMsg.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new OrderMsgAdapter(2);
        mAdapter.bindToRecyclerView(mRvMsgMsg);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.setOnLoadMoreListener(this, mRvMsgMsg);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }


    @Override
    protected void initData() {
        pageIndex = 1;
        mPresenter.getMsgList(pageIndex, pageSize,2);
    }

    @Override
    protected void initEvent() {

    }


    @Override
    public void showMsgListSuccess(List<MsgBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMsgListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showMsgListMoreSuccess(List<MsgBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showMsgListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showMsgListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }


    private void refresh() {
        mAdapter.setEnableLoadMore(false);
        initData();
    }

    private void loadMore() {
        mPresenter.getMsgListMore(pageIndex, pageSize,2);
    }




    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        
    }
}
